package com.hylu.springboot.system.controller;

import com.hylu.springboot.system.pojo.po.MenuDO;
import com.hylu.springboot.system.pojo.vo.ResultAjax;
import com.hylu.springboot.system.service.MenuService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 菜单管理
 *
 * @author hylu
 * @email 1325101787@qq.com
 * @date 2017-12-26 20:11:56
 */

@Controller
@RequestMapping("/system/menu")
public class MenuController {
    String prefix = "system/menu/";
    @Autowired
    private MenuService menuService;

    @GetMapping()
    String Menu() {
        return prefix + "menu";
    }

    @ResponseBody
    @GetMapping("/list")
    public List<MenuDO> list() {
        // 查询列表数据
        Map<String, Object> columnMap = new HashedMap();
        List<MenuDO> menuList = menuService.selectByMap(columnMap);
        return menuList;
    }

    @GetMapping("/add/{pId}")
    String add(Model model, @PathVariable("pId") Long pId) {
        model.addAttribute("pId", pId);

        if (pId == 0) {
            model.addAttribute("pName", "根目录");
        } else {
            model.addAttribute("pName", menuService.selectById(pId).getName());
        }

        return prefix + "/add";
    }

    @GetMapping("/edit/{id}")
    String edit(Model model, @PathVariable("id") Long id) {
        model.addAttribute("menu", menuService.selectById(id));

        return prefix + "/edit";
    }

    @PostMapping("/remove")
    @ResponseBody
    ResultAjax remove(Long id) {
        if (menuService.deleteById(id)) {
            return ResultAjax.ok();
        } else {
            return ResultAjax.error(1, "删除失败");
        }
    }

    @PostMapping("/save")
    @ResponseBody
    ResultAjax save(MenuDO menu) {
        if (menuService.insert(menu)) {
            return ResultAjax.ok();
        } else {
            return ResultAjax.error(1, "保存失败");
        }
    }


    @PostMapping("/update")
    @ResponseBody
    ResultAjax update(MenuDO menu) {
        if (menuService.updateById(menu)) {
            return ResultAjax.ok();
        } else {
            return ResultAjax.error(1, "更新失败");
        }
    }


}
